<?php
//红包管理
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);
pc_base::load_app_func('global');
class bonus extends admin {
	function __construct() {
		parent::__construct();
		pc_base::load_sys_class('form');
		$this->bonus = pc_base::load_model('ushop_bonus_model');
		$this->bonus_type = pc_base::load_model('ushop_bonus_type_model');
		$this->member = pc_base::load_model('member_model');
		$this->product = pc_base::load_model('ushop_product_model');
		$this->member_group = pc_base::load_model('member_group_model');
	}
	//红包类型
	public function init() {
		if(isset($_POST['sale_order']) && !empty($_POST['sale_order'])) {
			$orderby = explode('&',$_POST['sale_order']);
		}else{
			$orderby = array('type_id','DESC');
		}
  		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->bonus_type->listinfo('',$order = "$orderby[0] $orderby[1]", $page, $pages = '12');
		foreach($infos as $key => $value) {
			$where = array('bonus_type_id'=>$value['type_id']);
			$infos[$key]['send_time'] = count($this->bonus->select($where));
			$where = array('bonus_type_id'=>$value['type_id']);
			$infos[$key]['used_time'] = arraysum($this->bonus->select($where),'is_user');
			if($value['send_type'] == 2) {
				$infos[$key]['send_time'] = '--';
			}
			if(($value['send_type'] == 1) || ($value['send_type'] == 2)) {
				$infos[$key]['used_time'] = '已过期，请修改';
			}
			if((($value['send_type'] == 1) || ($value['send_type'] == 2)) && ((SYS_TIME > $value['use_start_date']) && (SYS_TIME < $value['use_end_date'])) ) {
				$infos[$key]['used_time'] = '可以使用';
			}
			switch($infos[$key]['send_type']) {
				case 0:
					$infos[$key]['send_type'] = "按用户发送";
					break;
				case 1:
					$infos[$key]['send_type'] = "按商品发放";	
					break;
				case 2:
					$infos[$key]['send_type'] = "按订单金额发送";
					break;
			}
			if(SYS_TIME >= $value['send_start_date'] and SYS_TIME <= $value['send_end_date'] ) {
				$infos[$key]['send_true'] = '1';
			}else{
				$infos[$key]['send_true'] = '0';
			}
		}
		$pages = $this->bonus_type->pages;
		include $this->admin_tpl('bonus');
	}
	//添加红包
	public function add_sale() {
		if(isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])){
			extract($_POST['search']);
			$info = $this->bonus_type->listinfo(array('type_name'=>$type_name,'send_type'=>$send_type));
			if(!empty($info)) {
				showmessage(L('类型名称已经存在！'),HTTP_REFERER);
			}
			$_POST['search']['send_start_date'] = strtotime($_POST['search']['send_start_date']);
			$_POST['search']['send_end_date'] = strtotime($_POST['search']['send_end_date']);
			$_POST['search']['use_start_date'] = strtotime($_POST['search']['use_start_date']);
			$_POST['search']['use_end_date'] = strtotime($_POST['search']['use_end_date']);
			$info = $this->bonus_type->insert($_POST['search'],true);
			if(!$info){
				showmessage(L('mass_failure'),HTTP_REFERER);
			}
			showmessage(L('operation_success'),'?m=ushop&c=sale&a=bonus','','edit');
		}else{
			$show_validator = $show_scroll = $show_header = true;
			include $this->admin_tpl('bonus_add');
		}
	}
	//红包类型操作中的 发放
	public function send_by() {
		$mid = intval($_GET['mid']); 
		if($mid < 1) return false;
		$type = intval($_GET['type']);
		switch($type) {
			//如果是按用户发放
			case "0":
				$show_validator = $show_scroll = $show_header = true;
				//会员列表
				$member = $this->member->listinfo();
				foreach($member as $key => $value) {
					$member_list[] = $value['username'];
				}
				//会员等级
				$member_group_infos = $this->member_group->select('','*','',$order = 'groupid ASC');
				include $this->admin_tpl('send_by_user');
				break;
			case "1":
				$show_validator = $show_scroll = $show_header = true;
				include $this->admin_tpl('send_by_merchandise');
				break;
		}
	}
	//按用户发送处理函数
	public function send_byuser() {
		$mid = intval($_POST['typeid']); 
		if($mid < 1) return false;
		$info = $this->bonus_type->get_one(array('type_id'=>$mid));
		//要添加的红包信息
		$array['bonus_type_id'] = $info['type_id'];
		$array['bonus_sn'] = 0;
		$array['used_time'] = 0;
		$array['order_id'] = 0;
		$array['emailed'] = 0;
		
		if(isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])){
			$user = $_POST['user'];
			//用户类型
			foreach($user as $member) {
				$member_count = $this->member->get_one(array('username'=>$member));
				$array['user_id'] = $member_count['userid'];
				$this->bonus->insert($array);
			}
			showmessage(L('operation_success'),'?m=ushop&c=sale&a=bonus','', 'edit');
		}elseif(isset($_POST['dosubmit0']) && !empty($_POST['dosubmit0'])){
			//用户类型
			$groupid = $_POST['rank_id'];
			$member_count = $this->member->listinfo(array('groupid'=>$groupid));
			foreach($member_count as $key => $value) {
				$array['user_id'] = $value['userid'];
				$this->bonus->insert($array);
			}
			showmessage(L('operation_success'),'?m=ushop&c=sale&a=bonus','', 'edit');
		}
	}
	//按商品发放 搜索
	public function search_merchandise() {
		if(isset($_POST['dosubmit'])) {
			$product_name = $_POST['merchanidse'];
			$sql = "select * from v9_ushop_product where title like '%$product_name%' ";
			$list_info = $this->product->query($sql);
			$i = 0;
			while($infos = mysql_fetch_array($list_info)) {
				$info[$i]['title'] = $infos['title'];
				$info[$i]['id'] = $infos['id'];
				$i++;
			}
			$mid = $_POST['typeid'];
		}
		$show_validator = $show_scroll = $show_header = true;
		include $this->admin_tpl('send_by_merchandise');
	}
	//按商品发放处理函数
	public function send_bymerchandise() {
		if(isset($_POST['dosubmit'])) {
			$id = intval($_POST['typeid']); 
			if($id < 1) return false;
			foreach($_POST['user'] as $value) {
				$infos = $this->product->get_one(array('id'=>$value));
				if(isset($infos['bonus_type_id']) && !empty($infos['bonus_type_id'])) {
					$mid = $infos['bonus_type_id'].":".$id;
				}else{
					$mid = $id;
				}
				$this->product->update(array('bonus_type_id'=>$mid),array('id'=>$value));
				$merchandisebonus['bonus_type_id'] = $id;
				$merchandisebonus['goods_id'] = $value;
				$this->bonus->insert($merchandisebonus,true);
			}
			showmessage(L('operation_success'),'?m=ushop&c=sale&a=bonus','', 'edit');
		}
	}
	//红包类型 操作中的 查看
	public function bonus_list() {
		$mid = intval($_GET['mid']); 
		if($mid < 1) return false;
		//发放类型
		$type = intval($_GET['type']);
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$where = array('bonus_type_id'=>$mid);
		switch($type) {
			case 0:
				$infos = $this->bonus->listinfo($where,$order = "bonus_id DESC", $page, $pages = '12');
				foreach($infos as $key => $value) {
					$info = $this->bonus_type->get_one(array('type_id'=>$value['bonus_type_id']));
					$member = $this->member->get_one(array('userid'=>$value['user_id']));
					$infos[$key]['title'] = $info['type_name'];
					$infos[$key]['member'] = $member['username'];
				}
				$pages = $this->bonus->pages;
				include $this->admin_tpl('bonus_list_user');
			break;
			case 1:
				$infos = $this->bonus->listinfo($where,$order = "bonus_id DESC", $page, $pages = '12');
				foreach($infos as $key => $value) {
					$goods_info[$key] = $this->product->get_one(array('id'=>$value['goods_id']));
					$goods_info[$key]['bonus_id'] = $value['bonus_id'];
				}
				$pages = $this->bonus->pages;
				include $this->admin_tpl('bonus_list_merchandise');
			break;
		}
	}
	//查看中的排序
	public function bonus_orderby() {
		$where = string2array(stripcslashes(stripcslashes($_POST['orderby'])));
		if(isset($_POST['sale_order']) && !empty($_POST['sale_order'])) {
			$orderby = explode('&',$_POST['sale_order']);
		}else{
			$orderby = array('bonus_id','DESC');
		}
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->bonus->listinfo($where,$order = "$orderby[0] $orderby[1]", $page, $pages = '12');
		foreach($infos as $key => $value) {
			$info = $this->bonus_type->get_one(array('type_id'=>$value['bonus_type_id']));
			$member = $this->member->get_one(array('userid'=>$value['user_id']));
			$infos[$key]['title'] = $info['type_name'];
			$infos[$key]['member'] = $member['username'];
		}
		if(isset($_POST['orderby_member']) && !empty($_POST['orderby_member'])) {
			switch($_POST['orderby_member']) {
				case type_name0:
					uasort($infos,order_by_title0);
					break;
				case type_name1:
					uasort($infos,order_by_title1);
					break;
				case user_id0:
					uasort($infos,order_by_member0);
					break;
				case user_id1:
					uasort($infos,order_by_member1);
					break;
			}
		}
		$pages = $this->bonus->pages;
		$type = $_GET['type'];
		switch($type) {
			case 0:
				include $this->admin_tpl('bonus_list_user');
				break;
			case 1:
				include $this->admin_tpl('bonus_list_merchandise');
				break;
			case 2:
				include $this->admin_tpl('bonus_list_merchandise');
				break;
		}
	}
	//红包 查看中的删除
	public function bonus_delete() {
		if((!isset($_GET['mid']) || empty($_GET['mid'])) && (!isset($_POST['mid']) || empty($_POST['mid']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['mid'])){
				foreach($_POST['mid'] as $mid_arr) {
					$this->bonus->delete(array('bonus_id'=>$mid_arr));
				}
				showmessage(L('operation_success'), HTTP_REFERER);
			}else{
				$mid = intval($_GET['mid']);
				if($mid < 1) return false;
				$result = $this->bonus->delete(array('bonus_id'=>$mid));
				if($result) {
					showmessage(L('operation_success'),HTTP_REFERER);
				} else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
	}
	//红包类型 操作中的 编辑
	public function edit() {
		$mid = intval($_GET['mid']); 
		if($mid < 1) return false;
		if(isset($_POST['dosubmit'])){ 
			$_POST['search']['send_start_date'] = strtotime($_POST['search']['send_start_date']);
			$_POST['search']['send_end_date'] = strtotime($_POST['search']['send_end_date']);
			$_POST['search']['use_start_date'] = strtotime($_POST['search']['use_start_date']);
			$_POST['search']['use_end_date'] = strtotime($_POST['search']['use_end_date']);
			$info = $this->bonus_type->update($_POST['search'],array('type_id'=>$mid));
			if(!$info){
				showmessage(L('mass_failure'),HTTP_REFERER);
			}
			showmessage(L('operation_success'),HTTP_REFERER,'','edit');
		}else{
 			$show_validator = $show_scroll = $show_header = true;
			$info = $this->bonus_type->get_one(array('type_id'=>$mid));
			if(!$info) showmessage('内容不存在');
			extract($info);
			include $this->admin_tpl('bonus_edit');
		}
	}
	//不打开编辑对话框，对数据进行编辑
	public function public_mouse_edit() {
		$mid = intval($_REQUEST['id']); 
		if($mid < 1) return false;
		if(empty($_POST['value'])) {
			echo "内容不能为空";
		}
		$value = $_POST['value'];
		switch($_POST['type']) {
			case 0:
				$this->bonus_type->update(array('type_name'=>$value),array('type_id'=>$mid));
			break;
			case 1:
				$this->bonus_type->update(array('type_money'=>$value),array('type_id'=>$mid));
			break;
			case 2:
				$this->bonus_type->update(array('min_amount'=>$value),array('type_id'=>$mid));
			break;
		}
	}
	//红包类型 操作中的 删除
	public function delete() {
		$show_validator = $show_scroll = $show_header = true;
		if((!isset($_GET['mid']) || empty($_GET['mid'])) && (!isset($_POST['mid']) || empty($_POST['mid']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['mid'])){
				foreach($_POST['mid'] as $mid_arr) {
					$this->bonus_type->delete(array('type_id'=>$mid_arr));
					$this->bonus->delete(array('bonus_type_id'=>$mid_arr));
				}
				showmessage(L('operation_success'), HTTP_REFERER);
			}else{
				$mid = intval($_GET['mid']);
				if($mid < 1) return false;
				$result0 = $this->bonus_type->delete(array('type_id'=>$mid));
				$result1 = $this->bonus->delete(array('bonus_type_id'=>$mid));
				if($result0 && $result1) {
					showmessage(L('operation_success'),HTTP_REFERER);
				} else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
	}
	//删除商品红包
	public function goods_delete() {
		$show_validator = $show_scroll = $show_header = true;
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['id'])){
				foreach($_POST['id'] as $mid_arr) {
					$this->bonus_type->delete(array('id'=>$mid_arr));
					$this->bonus->delete(array('id'=>$mid_arr));
				}
				showmessage(L('operation_success'), HTTP_REFERER);
			}else{
				$id = intval($_GET['id']);
				$bonusid = intval($_GET['bonusid']);
				$bonus_id = intval($_GET['bonus_id']);
				if($id < 1) return false;
				$this->bonus->delete(array('bonus_id'=>$bonus_id));
				
				$goodsinfo = $this->product->get_one(array('id'=>$id));
				$goodbonustype = explode(":",$goodsinfo['bonus_type_id']);
				foreach($goodbonustype as $key => $value) {
					if($value == $bonusid ) {
						unset($goodbonustype[$key]);
					}
				}
				$good_bouns_type = implode(":",$goodbonustype);
				
				$result = $this->product->update(array('bonus_type_id'=>$good_bouns_type),array('id'=>$id));
				
				if($result) {
					showmessage(L('operation_success'),HTTP_REFERER);
				} else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
	}
	//给会员发送邮件
	public function public_send_email() {
		$mid = intval($_GET['mid']);
		if($mid < 0) return false;
		$user_id = $_GET['user_id'];
		$user_email = get_memberinfo($user_id,'email');
		$txt ="=?UTF-8?B?".base64_encode('激活成功')."?=";
		pc_base::load_app_class('smtp');
		$smtp = new smtp('smtp.126.com','25',true,'ningpanyun','wangning');
		$smtp->sendmail($user_email,'ningpanyun@126.com',$txt,'激活成功','HTML');
		$this->bonus->update(array('emailed' => 1),array('bonus_id'=>$mid));
	}
}
?>